package tech.aistar.prj.controller;

import com.alibaba.fastjson.JSONArray;
import tech.aistar.prj.entity.Pet;
import tech.aistar.prj.entity.PetType;
import tech.aistar.prj.entity.Result;
import tech.aistar.prj.service.IPetService;
import tech.aistar.prj.service.IPetTypeService;
import tech.aistar.prj.service.impl.PetServiceImpl;
import tech.aistar.prj.service.impl.PetTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 本类功能:
 *
 * @author cxylk
 * @date 2020/9/18 18:22
 */
@WebServlet(urlPatterns = "/pet/*")
public class PetHandlerAction extends HttpServlet {
    IPetService petService=new PetServiceImpl();
    private IPetTypeService petTypeService=new PetTypeServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo=req.getPathInfo();
        switch (pathInfo){
            case "/del":
                String sid=req.getParameter("sids");
                String[] sids=sid.split(":");
                if(null!=sids&&sids.length>0){
                    for(String id:sids){
                        petService.delById(Integer.valueOf(id));
                    }
                }
                //此处用重定向，用转发的话会删两次
                //req.getRequestDispatcher("/pet/indexData").forward(req,resp);
                resp.sendRedirect("/lk/pet/indexData");
                break;
            case "/indexData":
                //        List<Pet> petList=petService.findAll();

                //必须要判断，因为long是包装类型，为空的话会包空指针异常
                //1.如果第一次进来，因为没有选中，所有typeId是null
                //2.如果下拉框有可能选择-1(即默认设置的-1)
                String typeId=req.getParameter("type_id");

                //放入service中进行逻辑业务判断
//        //为空就让它等于-1，因为-1就是默认的值
//        typedId=typeId==null?"-1":typeId;
//        //String转换成long
//        Long tid=Long.valueOf(typeId);

                //3.接收搜索框
                String name=req.getParameter("name");


                //但是数据库中是没有-1这个id的，所有要去修改查询语句，进行判断
//        List<Pet> petList=petService.search(typeId,null);
                List<Pet> petList=petService.search(typeId,name);

                List<PetType> petTypes=petTypeService.getAll();

                req.setAttribute("pets",petList);
                req.setAttribute("types",petTypes);

                //为了进行下拉框的一个数据回显的功能，刚刚传过来的typeId继续回显到页面上
                req.setAttribute("typeId",typeId==null?"-1":typeId);
                req.setAttribute("name",name==null?"":name);

                req.getRequestDispatcher("/pet/view").forward(req,resp);
                break;
            case "/del_one":
                String id=req.getParameter("id");
                petService.delById(Integer.valueOf(id));

                //为了模拟网络延迟
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                resp.sendRedirect("/lk/pet/indexData");
                break;
            case "/del_ajax":
                resp.setContentType("text/json;charset=utf-8");
                PrintWriter out=resp.getWriter();
                String ids=req.getParameter("id");
                int count=petService.delById(Integer.valueOf(ids));
//                int count=0;
                Result result;
                if(count==0){//删除失败
                    result=new Result("500","删除失败");
                }else{
                    result=new Result("200","删除成功");
                }
                String str= JSONArray.toJSONString(result);
//                try {
//                    Thread.sleep(3000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                out.print(str);//将json字符串返回给页面...
                break;
        }
    }
}
